package com.m3958.firstgwt.server.utils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.server.service.SiteConfiger;
import com.m3958.firstgwt.server.service.SiteConfiger.SiteConfigerKeyName;


@Singleton
public class WriteErrorForMe {
	
	@Inject
	private SiteConfiger scf;
	
	public void writeError(Exception e,String fn){
		try {
		    StringWriter sw = new StringWriter();
		    PrintWriter pw = new PrintWriter(sw);
		    e.printStackTrace(pw);
		    File ef = new File(scf.getValue(SiteConfigerKeyName.APP_ERROR_DIR));
		    ef = new File(ef,fn);
		    Files.createParentDirs(ef);
			Files.write(sw.toString(), ef, Charsets.UTF_8);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
}
